var searchResult = {};
searchResult.methods = {};
searchResult.pageNumCallback = null;
searchResult.OrderSortCallback = null;

$(function(){
    //多选
    searchResult.methods.classChooses();
    //是否套装
    searchResult.methods.isSuit();
    // 分类\品牌\出版\套装---展开闭合
    searchResult.methods.classifyMore();
    //综合,销量,好评
    searchResult.methods.termTxtSelect();
    //价格排序
    searchResult.methods.termPriceSelect();
    //价格筛选
    searchResult.methods.termPriceMoney();
    // 配送至
    searchResult.methods.termSend();
    // 自营、仅显示有货、促销 
    searchResult.methods.termStatue();
    //大图,列表切换
    searchResult.methods.productChange();
    //星星
    searchResult.methods.imageListStar();
     //筛选右侧上页、下页 回调
    searchResult.methods.smallPage();
    //关注
    searchResult.methods.isStore();
    //初始化tab
    searchResult.methods.tabInit(this);
    //初始化切换
    searchResult.methods.sliderInit(this);
    // 筛选条件冻结
    searchResult.methods.columnFixed();
    // 点击排序返回序号
    searchResult.methods.termNumReturn();

    // 分页
    var page = new Controller.PageControl();
    page.init({
        totalCount: 100, //总页数
        conter: ".shop-page", //容器选择器
        nowPage: 1 //当前页，默认第几页
        // callback:fun,      
        // gotoCallBack:fun   
    });
})


//多选
searchResult.methods.classChooses = function(){
    $(".shop-term-classify-con").find("a").attr({"href":"javascript:void(0);","target":"_self"});
    $(".shop-term-classify-con").find("a").on("click",function(){
        $(this).toggleClass("shop-txt-on");
    })
}

//是否套装
searchResult.methods.isSuit = function(){
    $(".shop-isSuit > span").on("click",function(){
        $(this).toggleClass("shop-noYes-on").siblings("span").removeClass("shop-noYes-on");
    })
}

// 分类\品牌\出版\套装---展开闭合
searchResult.methods.classifyMore = function(){
    $(".shop-classifty-more").find("span").on("click", function () {
        if($(this).hasClass("shop-classifty-more-top")){
            $(this).removeClass("shop-classifty-more-top");
            $(this).removeClass("shop-classifty-more-top-red");
            $(this).addClass("shop-classifty-more-bot-red");
            $(this).text("更多");
            $(this).parents("li").css("height","42px");
        }else{
            $(this).addClass("shop-classifty-more-top");
            $(this).addClass("shop-classifty-more-top-red");
            $(this).text("收起");
            $(this).parents("li").css("height","auto");
            $(this).parents("li").find(".shop-term-classify-title").height($(this).parents("li").height());
            
        }
    });

    $(".shop-classifty-more").find("span").hover(function(){
        if($(this).hasClass("shop-classifty-more-top")){
            $(this).addClass("shop-classifty-more-top-red");
        }else if($(this).hasClass("shop-classifty-more-bot")){
            $(this).addClass("shop-classifty-more-bot-red");
        }
    },function(){
        $(this).removeClass("shop-classifty-more-bot-red");
        $(this).removeClass("shop-classifty-more-top-red");
    });
}

//综合,销量,好评
searchResult.methods.termTxtSelect = function(){
	$(".shop-term-txt-select").on("click",function(){
		$(this).addClass("shop-term-txt-select-on").siblings(".shop-term-txt-select").removeClass("shop-term-txt-select-on");
		$(".shop-term-price-select").find(".shop-price-up-corner").attr("src",imgRoot+"/static/search_arrow_price_up_default.png");
        $(".shop-term-price-select").find(".shop-price-down-corner").attr("src",imgRoot+"/static/search_arrow_price_down_default.png");
        $(".shop-term-price-select").removeClass("shop-term-price-select-on");
    });
}

//价格排序
searchResult.methods.termPriceSelect = function(){
	var flag = true;  
    $(".shop-term-price-select").on("click",function(){
		$(".shop-term-txt-select").removeClass("shop-term-txt-select-on");
		$(this).addClass("shop-term-price-select-on");
		if(flag){
			$(this).find(".shop-price-up-corner").attr("src",imgRoot+"/static/search_arrow_price_up_sel.png");
			$(this).find(".shop-price-down-corner").attr("src",imgRoot+"/static/search_arrow_price_down_default.png");
			flag = false;
		}else if(flag == false){
			$(this).find(".shop-price-up-corner").attr("src",imgRoot+"/static/search_arrow_price_up_default.png");
			$(this).find(".shop-price-down-corner").attr("src",imgRoot+"/static/search_arrow_price_down_sel.png");
		    flag = true;
        }
	})
}
//价格筛选
searchResult.methods.termPriceMoney = function(){
	$(".shop-term-price .shop-input-price").on("focus",function(){
        $(".shop-term-price-bg").show();
        $(".shop-term-price-btn").show();
        $(".shop-input-price").css("border","1px #e64545 solid");
    })
    $(".shop-term-price .shop-input-price").on("blur",function(){
        $(".shop-term-price-btn").hide();
        $(".shop-term-price-bg").hide();
        $(".shop-input-price").css("border","1px #e1e1e1 solid");
    })
	$(".shop-price-sure").on("click",function(){
        $(".shop-term-price-btn").hide();
        $(".shop-term-price-bg").hide();
        $(".shop-input-price").css("border","1px #e1e1e1 solid");
    })
    $(".shop-price-cancel").on("mousedown",function(e){
        e.preventDefault();
        $(".shop-input-price").val("");
    })
}
// 配送至
searchResult.methods.termSend = function(){
    $(".shop-term-send-con li").on("click",function(){
        $(".shop-term-send-con li").removeClass("shop-position-activity");
        $(this).addClass("shop-position-activity");
        $(".shop-term-send-txt span").text($(this).text());
    })
}
// 自营、仅显示有货、促销 
searchResult.methods.termStatue = function(){
    $(".shop-term-statue ul").hover(function(){
        $(".shop-term-statue").removeClass("shop-term-statue-hidden");
        $(".shop-term-statue ul").css({"background-color": "#fff", "outline": "1px #eee solid"})
    },function(){
        $(".shop-term-statue").addClass("shop-term-statue-hidden");
        $(".shop-term-statue ul").css({"background": "none", "outline":0})
    })
}


//大图,列表切换
searchResult.methods.productChange = function(){
	$(".shop-change-imageList").on("click",function(){
        $(this).addClass("shop-change-imageList-on").siblings(".shop-change-bigImage").removeClass("shop-change-bigImage-on");
		$(".shop-product-row").hide();
		$(".shop-product-col").show();
		$(".shop-book-show-good").css("height","2153px");
	})
	$(".shop-change-bigImage").on("click",function(){
        $(this).addClass("shop-change-bigImage-on").siblings(".shop-change-imageList").removeClass("shop-change-imageList-on");
		$(".shop-product-row").show();
		$(".shop-product-col").hide();
		$(".shop-book-show-good").css("height","2295px");
	})
}

//星星
searchResult.methods.imageListStar = function(){
    $(".shop-product-col-stars i").each(function(){
        var starNum =  parseInt($(this).text());
        if(starNum < 1 || starNum > 5){
            $(this).text("");
            $(this).css("width",0);
            return;
        }else{
            var newStarNum = ((starNum*14.8)/74*100) + "%";
            $(this).css("width",newStarNum);
            $(this).text("");
        }
    })
}

//筛选右侧上页、下页
searchResult.methods.smallPage = function smallPage(){

    $(".shop-term-page-btn-left").on("click", function () {

        var dataFirst = parseInt($(".shop-term-data-first").text());
        var dataLast = parseInt($(".shop-term-data-last").text());
        searchResult.methods.smallPageChange(dataFirst, dataLast);

		if(dataFirst > 1){
			dataFirst--;
			searchResult.methods.smallPageChange(dataFirst,dataLast);
			
		}else{
			searchResult.methods.smallPageChange(dataFirst,dataLast);
            dataFirst = dataFirst;
			return;
        }
    
        $(".shop-term-data-first").text(dataFirst);
        if (searchResult.pageNumCallback != null) {
            if (typeof (searchResult.pageNumCallback) == "function") {
                searchResult.pageNumCallback(dataFirst)
            } else {
                console.log("您传递的参数有误");
            }
        }
		
	});
    $(".shop-term-page-btn-right").on("click", function () {

        var dataFirst = parseInt($(".shop-term-data-first").text());
        var dataLast = parseInt($(".shop-term-data-last").text());
        searchResult.methods.smallPageChange(dataFirst, dataLast);

		if(dataFirst < dataLast){
			dataFirst++;
			searchResult.methods.smallPageChange(dataFirst,dataLast);
		}else{
			searchResult.methods.smallPageChange(dataFirst,dataLast);
			dataFirst = dataLast;
			return;
        }
    
        $(".shop-term-data-first").text(dataFirst);
        if (searchResult.pageNumCallback != null) {
            if (typeof (searchResult.pageNumCallback) == "function") {
                searchResult.pageNumCallback(dataFirst)
            } else {
                console.log("您传递的参数有误");
            }
        }
		
	});
}
searchResult.methods.smallPageChange= function(dataFirst,dataLast){
	if(dataFirst > 1 && dataFirst < dataLast){
		$(".shop-term-page-btn-left").find("img").attr("src",imgRoot+"/static/search_ico_left.png");
		$(".shop-term-page-btn-right").find("img").attr("src",imgRoot+"/static/search_ico_right.png");
	}
	if(dataFirst <= 1){
		$(".shop-term-page-btn-left").find("img").attr("src",imgRoot+"/static/search_ico_left_gray.png");
		$(".shop-term-page-btn-right").find("img").attr("src",imgRoot+"/static/search_ico_right.png");
	}
	if(dataFirst >= dataLast){
		$(".shop-term-page-btn-right").find("img").attr("src",imgRoot+"/static/search_ico_right_gray.png");
		$(".shop-term-page-btn-left").find("img").attr("src",imgRoot+"/static/search_ico_left.png");
	}
}

// 关注
searchResult.methods.isStore = function(){
    $(".shop-product-row-bot-focus").find("span").on("click",function(){
        if($(this).hasClass("shop-car-activity")){
            $(this).removeClass("shop-car-activity");
        }else{
            $(this).addClass("shop-car-activity");
        }
    });
}

//初始化tab
searchResult.methods.tabInit = function (obj) {
    $(".shop-tabcontrol").each(function (val) {
        var tab = new Controller.TabControl({
            container: $(this),
            tabNavItem: ".shop-tab-nav-item",
            tabConItem: ".shop-tab-con-item",
            tagName: "data-id",
            event: "click"
        });
    });
};
//初始化切换
searchResult.methods.sliderInit = function (obj) {
    $(".shop-tab-con-item").each(function (val) {
        Controller.Slider({
            father: $(this),
            container: ".shop-goods-list ul",
            prevBtn: ".shop-left-nav",
            nextBtn: ".shop-right-nav",
            itemSelector: "li",
            fatherSize: 1440
        });
    });
};

// 筛选条件冻结
searchResult.methods.columnFixed = function () {
    var chooseTermScrollTop = $(".shop-choose-term").offset().top;
    var scrollH = 0;
    $(window).on("scroll", function () {
        scrollH = $(this).scrollTop();
        if (scrollH >= chooseTermScrollTop) {
            $(".shop-choose-term-fixed").show();
            $(".shop-choose-term").addClass("shop-choose-term-fixed-show");
        } else if (scrollH < chooseTermScrollTop) {
            $(".shop-choose-term-fixed").hide();
            $(".shop-choose-term").removeClass("shop-choose-term-fixed-show"); 
        }
    })
}

//返回数字
searchResult.methods.termNumReturn = function () {

    var termIndex = 0;
    var priceFlag = true;
    $(".shop-term-txt-select").on("click", function () {
        if (searchResult.OrderSortCallback == null && typeof (searchResult.OrderSortCallback) != "function") {
            return;
        }
        termIndex = $(".shop-term-txt-select").index($(this));
        termIndex = termIndex + 1;
        searchResult.OrderSortCallback(termIndex);
    })
    $(".shop-term-price-select").on("click", function () {
        if (searchResult.OrderSortCallback == null && typeof (searchResult.OrderSortCallback) != "function") {
            return;
        }
        termIndex = 0;
        if (priceFlag) {
            termIndex = termIndex + 4;
            priceFlag = false;
        } else {
            termIndex = termIndex + 5;
            priceFlag = true;
        }
        searchResult.OrderSortCallback(termIndex);
    })
}
